'use strict';
var app = {
	init : function() {
		var url_default = {clinical: 'patient', setting: 'user'};
		var thisUrl = $("#thisUrl").val().split('/'),
			first = thisUrl[0],
			second = (thisUrl.length > 1) ? '/' + thisUrl[1] : '/' + url_default[first],
			third = (thisUrl.length > 2) ? '/' + thisUrl[2] : '';

		$("a[href='/" + first + "']").attr("class", "nav__item-link is-active active-red");
		$("ul[class='nav nav-tabs']").find($("a[href^='/" + first + second + third + "']")).parent().attr("class", "active");

        helper.date_ymd();
	}
},
helper = {
	spaceDrop : function(text) {
		text = text.replace(/(^\s+)|(\s*)|(\s+$)/g, "");
		return text;
	},
	underlineDrop :function(text) {
		text = text.replace(/_*(-|:)_*/g, "");
		return text;
	},
	date_ymd : function() {
		$.datetimepicker.setLocale('ch');

		jQuery('.laydate-icon.date').datetimepicker({
			yearStart : 1900,
			mask : '2999-19-39',
			format : 'Y-m-d',
			timepicker : false,
			scrollMonth: false,
			scrollTime: false,
			scrollInput: false,
			validateOnBlur: true,
		});

		jQuery('.laydate-icon.date-y-m').datetimepicker({
			yearStart : 1900,
			mask : '2999-19',
			format : 'Y-m',
			timepicker : false,
			scrollMonth: false,
			scrollTime: false,
			scrollInput: false,
			validateOnBlur: false,
		});

        jQuery('.laydate-icon.time').datetimepicker({
            mask : '29:59',
            format : 'H:i',
            datepicker : false,
            step : 5
        });
	},
	date_today : function() {
		var today = new Date();
		var current = today.getFullYear() + '-' + today.getMonth() + '-' + today.getDate();
		return current;
	},
	errorPop : function(text) {
		var errorDiv = $("#error-msg");
		layer.msg(text);
		errorDiv.css('display', 'block');
		errorDiv.html(text);
	},
	detect : function(inputs) {
		if (inputs.length == 0) return true;

		console.log('inputs');

		var _this = this,
				error = 0;
		$.each(inputs, function() {

			var thisValue = $(this).val(),
				thisLabel = $(this).siblings("label").html();

			thisLabel = (thisLabel == undefined) ? '' : thisLabel;
			thisValue = _this.spaceDrop(thisValue);
			thisValue = _this.underlineDrop(thisValue);
			if (thisValue == "") {

				_this.ifHasTimeLine(this);

				$(this).focus();
				_this.errorPop(thisLabel + "不能为空或格式有误");
				error ++;
				return false;
			}
		});

		if (error != 0) {
			return false;
		}
		return true;
	},
	detectRadio : function(radio) {
		if (radio.length == 0) return true;

		console.log('radio');

		var _this = this,
				error = 0;
		$.each(radio, function() {
			var	thisLabel = $(this).siblings("label").html();

			if ($(this).find('input:checked').length == 0) {
				_this.ifHasTimeLine(this);

				$(this).find('input').focus();
				_this.errorPop(thisLabel + "不能为空或格式有误");
				error ++;
				return false;
			}
		});

		if (error != 0) {
			return false;
		}
		return true;
	},
	detectSelect : function(select) {
		if (select.length == 0) return true;

		console.log('select');

		var _this = this,
				error = 0;
		$.each(select, function() {
			if (this.value == '') {
				this.focus();
				_this.errorPop("任何一项不能为空");
				error += 1;
				return false;
			}
		});

		if (error != 0) {
			return false;
		}

		return true;
	},
	ifHasTimeLine : function(input) {
		var thisLi = $(input).parent().parent().attr('data-date');

		if (typeof(thisLi) == undefined) return ;

		$("#timelineList li a").each(function() {
			if ($(this).attr('data-date') == thisLi) {
				if ($(this).attr('class') != 'selected') {
					$(this).click();
				}
				return false;
			}
		});
	},
	ajax_get: function(url) {
		$.get(url, function(data) {
			alert(data);
		});
	}
};

(function() {
	app.init();
})();;